package com.yx.cloud.product.gyro;

import com.google.gson.Gson;
import com.iot.base.bean.Device;
import com.iot.common.utils.LogUtils;
import com.iot.common.utils.StringUtils;
import com.iot.product.sweeper.bean.Appointment;
import com.iot.sdk.yugong.YuGongConstants;
import com.iot.sdk.yugong.bean.UndisturbedListBean;
import com.iot.sdk.yugong.req.YuGongGyroAddAppointment;
import com.iot.sdk.yugong.req.YuGongGyroModifyAppointment;
import com.iot.sdk.yugong.req.YuGongPayloadAddNoDisturb;
import com.iot.sdk.yugong.req.YuGongPayloadModifyAppointment;
import com.iot.sdk.yugong.req.YuGongPayloadModifyNoDisturb;
import com.iot.sweeper.param.SetQuiteTimeParam;

import java.util.ArrayList;
import java.util.List;

public class GyroDataConverter {
    public static YuGongGyroAddAppointment getAddAppointmentPayload(Appointment appointment) {
        /*
        {
            "opt": "auto_task_add",
            "week_repeat": [
              1,
              2,
              3
            ],
            "cmd_content": {
              "working_status": "AutoClean"
            },
            "timer_minute": "50",
            "timer_hour": "03",
            "object_id": "spark-980-G1-86-16-e1-44-fe"
        }
        */
        YuGongGyroAddAppointment yuGongGyroAddAppointment = new YuGongGyroAddAppointment();
        yuGongGyroAddAppointment.setOpt(YuGongConstants.OPT_ADD_APPOINTMENT);

        int hour = appointment.getTime() / 3600;
        int min = appointment.getTime() % 3600 / 60;

        String timer = hour + ":" + min;
        String local2UTC = StringUtils.Local2UTC(timer);//要把系统时区时间转换成UTC时间下发
        String[] split = local2UTC.split(":");
        yuGongGyroAddAppointment.setTimer_hour(split[0]);
        yuGongGyroAddAppointment.setTimer_minute(split[1]);

        YuGongGyroAddAppointment.CmdContentBean cmdContentBean = new YuGongGyroAddAppointment.CmdContentBean();
        int cleanMode = appointment.getCleanMode();
        if (0 == cleanMode) {
            //全屋
            cmdContentBean.setWorking_status("AutoClean");
        } else if (1 == cleanMode) {
            //沿边
            cmdContentBean.setWorking_status("EdgeClean");
        }else if(2 == cleanMode){
            //局部
            cmdContentBean.setWorking_status("SpotClean");
        }
        yuGongGyroAddAppointment.setCmd_content(cmdContentBean);

        int fan_status = appointment.getCleanParam().suction;
        if (1 == fan_status) {
            cmdContentBean.setFan_status("Quiet");
        } else if (2 == fan_status) {
            cmdContentBean.setFan_status("Normal");
        } else if (3 == fan_status) {
            cmdContentBean.setFan_status("Strong");
        } else if (4 == fan_status) {
            cmdContentBean.setFan_status("Max");
        }

        yuGongGyroAddAppointment.setWeek_repeat(getGyroRepeat(appointment.getRepeat()));

        LogUtils.i("预约 添加 ：" + new Gson().toJson(yuGongGyroAddAppointment));
        return yuGongGyroAddAppointment;
    }

    public static List<Integer> getGyroRepeat(List<Integer> repeat){
        List<Integer> gyroRepeat = new ArrayList<>();
        for(int i = 0;i < repeat.size(); i++){
            gyroRepeat.add((repeat.get(i) + 6) % 7);
        }
        return gyroRepeat;
    }

    public static List<Integer> getRepeat(List<Integer> gyroRepeat){
        List<Integer> repeat = new ArrayList<>();
        for(int i = 0;i < gyroRepeat.size(); i++){
            repeat.add((gyroRepeat.get(i) + 1) % 7);
        }
        return repeat;
    }

    public static YuGongGyroModifyAppointment getModifyAppointmentPayload(Appointment appointment) {
        /*
       {
    "opt": "auto_task_update",
    "auto_id": "560eaff90e4044b39647f3d7538287f3",
    "week_repeat": [
      0
    ],
    "cmd_content": {
      "working_status": "on"
    },
    "timer_minute": "13",
    "timer_hour": "04",
    "object_id": "spark-980-G1-86-16-e1-44-fe"
  }
        */
        YuGongGyroModifyAppointment yuGongGyroModifyAppointment = new YuGongGyroModifyAppointment();
        yuGongGyroModifyAppointment.setOpt(YuGongConstants.OPT_MODIFY_APPOINTMENT);
        yuGongGyroModifyAppointment.setAuto_id(appointment.getAutoId());

        int hour = appointment.getTime() / 3600;
        int min = appointment.getTime() % 3600 / 60;

        String timer = hour + ":" + min;
        String local2UTC = StringUtils.Local2UTC(timer);//要把系统时区时间转换成UTC时间下发
        String[] split = local2UTC.split(":");
        yuGongGyroModifyAppointment.setTimer_hour(split[0]);
        yuGongGyroModifyAppointment.setTimer_minute(split[1]);

        YuGongGyroModifyAppointment.CmdContentBean cmdContentBean = new YuGongGyroModifyAppointment.CmdContentBean();
        int cleanMode = appointment.getCleanMode();
        if (0 == cleanMode) {
            //全屋
            cmdContentBean.setWorking_status("AutoClean");
        } else if (1 == cleanMode) {
            //沿边
            cmdContentBean.setWorking_status("EdgeClean");
        }else if(2 == cleanMode){
            //局部
            cmdContentBean.setWorking_status("SpotClean");
        }
        yuGongGyroModifyAppointment.setCmd_content(cmdContentBean);
        yuGongGyroModifyAppointment.setObject_id(appointment.getId()+"");

        int fan_status = appointment.getCleanParam().suction;
        if (1 == fan_status) {
            cmdContentBean.setFan_status("Quiet");
        } else if (2 == fan_status) {
            cmdContentBean.setFan_status("Normal");
        } else if (3 == fan_status) {
            cmdContentBean.setFan_status("Strong");
        } else if (4 == fan_status) {
            cmdContentBean.setFan_status("Max");
        }

        yuGongGyroModifyAppointment.setWeek_repeat(getGyroRepeat(appointment.getRepeat()));

        LogUtils.i("预约 修改 ：" + new Gson().toJson(yuGongGyroModifyAppointment));
        return yuGongGyroModifyAppointment;
    }

    public static YuGongPayloadAddNoDisturb getAddNoDisturbPayload(SetQuiteTimeParam param, Device device) {
        YuGongPayloadAddNoDisturb yuGongPayloadAddNoDisturb = new YuGongPayloadAddNoDisturb();
        yuGongPayloadAddNoDisturb.setOpt(YuGongConstants.OPT_ADD_NO_DISTURB);
        yuGongPayloadAddNoDisturb.setObject_id(device.getDeviceId());

        List<YuGongPayloadAddNoDisturb.UndisturbedListBean> undisturbedListBeans = new ArrayList();

        YuGongPayloadAddNoDisturb.UndisturbedListBean undisturbedListBeanStart = new YuGongPayloadAddNoDisturb.UndisturbedListBean();
        undisturbedListBeanStart.setUndisturbed_name("undisturbed_start");
        undisturbedListBeanStart.setObject_id(device.getDeviceId());
        String timerStart = param.getStartHour() + ":" + param.getStartMin();
        String localStart2UTC = StringUtils.Local2UTC(timerStart);//要把系统时区时间转换成UTC时间下发
        String[] start = localStart2UTC.split(":");
        undisturbedListBeanStart.setTimer_hour(start[0]);
        undisturbedListBeanStart.setTimer_minute(start[1]);
        undisturbedListBeanStart.setWeek_repeat(getGyroRepeat(param.getRepeat()));
        YuGongPayloadAddNoDisturb.UndisturbedListBean.CmdContentBean cmdContentBean = new YuGongPayloadAddNoDisturb.UndisturbedListBean.CmdContentBean();
        cmdContentBean.setUndisturb_mode("on");
        undisturbedListBeanStart.setCmd_content(cmdContentBean);
        undisturbedListBeans.add(undisturbedListBeanStart);

        YuGongPayloadAddNoDisturb.UndisturbedListBean undisturbedListBeanEnd = new YuGongPayloadAddNoDisturb.UndisturbedListBean();
        undisturbedListBeanEnd.setUndisturbed_name("undisturbed_end");
        undisturbedListBeanEnd.setObject_id(device.getDeviceId());
        String timerEnd = param.getEndHour() + ":" + param.getEndMin();
        String localEnd2UTC = StringUtils.Local2UTC(timerEnd);//要把系统时区时间转换成UTC时间下发
        String[] end = localEnd2UTC.split(":");
        undisturbedListBeanEnd.setTimer_hour(end[0]);
        undisturbedListBeanEnd.setTimer_minute(end[1]);
        undisturbedListBeanEnd.setWeek_repeat(getGyroRepeat(param.getRepeat()));
        YuGongPayloadAddNoDisturb.UndisturbedListBean.CmdContentBean cmdContentBean1 = new YuGongPayloadAddNoDisturb.UndisturbedListBean.CmdContentBean();
        cmdContentBean1.setUndisturb_mode("off");
        undisturbedListBeanEnd.setCmd_content(cmdContentBean1);
        undisturbedListBeans.add(undisturbedListBeanEnd);

        yuGongPayloadAddNoDisturb.setUndisturbed_list(undisturbedListBeans);

        return yuGongPayloadAddNoDisturb;
    }

    public static YuGongPayloadModifyNoDisturb getModifyNoDisturbPayload(SetQuiteTimeParam param, Device device) {
        YuGongPayloadModifyNoDisturb yuGongPayloadModifyNoDisturb = new YuGongPayloadModifyNoDisturb();
        yuGongPayloadModifyNoDisturb.setOpt(YuGongConstants.OPT_MODIFY_NO_DISTURB);
        yuGongPayloadModifyNoDisturb.setObject_id(device.getDeviceId());
        yuGongPayloadModifyNoDisturb.setUndisturbed_id(param.getUndisturbedId());

        List<UndisturbedListBean> undisturbedListBeans = new ArrayList();

        UndisturbedListBean undisturbedListBeanStart = new UndisturbedListBean();
        undisturbedListBeanStart.setUndisturbed_name("undisturbed_start");
        undisturbedListBeanStart.setObject_id(device.getDeviceId());
        String timerStart = param.getStartHour() + ":" + param.getStartMin();
        String localStart2UTC = StringUtils.Local2UTC(timerStart);//要把系统时区时间转换成UTC时间下发
        String[] start = localStart2UTC.split(":");
        undisturbedListBeanStart.setTimer_hour(start[0]);
        undisturbedListBeanStart.setTimer_minute(start[1]);
        undisturbedListBeanStart.setCond_id(param.getStartCondId());
        undisturbedListBeanStart.setWeek_repeat(getGyroRepeat(param.getRepeat()));
        UndisturbedListBean.CmdContentBean cmdContentBean = new UndisturbedListBean.CmdContentBean();
        cmdContentBean.setUndisturb_mode("on");
        undisturbedListBeanStart.setCmd_content(cmdContentBean);
        undisturbedListBeans.add(undisturbedListBeanStart);

        UndisturbedListBean undisturbedListBeanEnd = new UndisturbedListBean();
        undisturbedListBeanEnd.setUndisturbed_name("undisturbed_end");
        undisturbedListBeanEnd.setObject_id(device.getDeviceId());
        String timerEnd = param.getEndHour() + ":" + param.getEndMin();
        String localEnd2UTC = StringUtils.Local2UTC(timerEnd);//要把系统时区时间转换成UTC时间下发
        String[] end = localEnd2UTC.split(":");
        undisturbedListBeanEnd.setTimer_hour(end[0]);
        undisturbedListBeanEnd.setTimer_minute(end[1]);
        undisturbedListBeanEnd.setCond_id(param.getEndCondId());
        undisturbedListBeanEnd.setWeek_repeat(getGyroRepeat(param.getRepeat()));
        UndisturbedListBean.CmdContentBean cmdContentBean1 = new UndisturbedListBean.CmdContentBean();
        cmdContentBean1.setUndisturb_mode("off");
        undisturbedListBeanEnd.setCmd_content(cmdContentBean1);
        undisturbedListBeans.add(undisturbedListBeanEnd);

        yuGongPayloadModifyNoDisturb.setUndisturbed_list(undisturbedListBeans);

        return yuGongPayloadModifyNoDisturb;
    }
}
